<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="660px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="formData.name" placeholder="请输入产品名称" :maxlength="100" />
                </el-form-item>
                <el-form-item label="合伙人" prop="partner_id">
                            <el-select
                                class="w-80"
                                v-model="formData.partner_id"
                                placeholder="请选择合伙人"
                                clearable
                                disabled
                            >
                                <el-option
                                    v-for="item in optionsData.partner_cate"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"
                                />
                            </el-select>
                        </el-form-item>
                
                <el-form-item label="图片" prop="team_picture">
                    <div>
                        <div>
                            <material-picker v-model="formData.team_picture" :limit="1" />
                        </div>
                        <div class="form-tips">建议尺寸：240*180px</div>
                    </div>
                </el-form-item>
                <el-form-item label="标语" prop="team_slogan">
                    <el-input v-model="formData.team_slogan" placeholder="请输入标语" />
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="formData.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'

import { partnerCateAll } from '@/api/partner'
import { teamAdd, teamDetail, teamEdit } from '@/api/team'
import { useDictOptions } from '@/hooks/useDictOptions'
import Popup from '@/components/popup/index.vue'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑团队' : '新增团队'
})
const formData = reactive({
    id: '',
    name: '',
    partner_id: '',
    team_picture: '',
    team_slogan: '',
    status: 1
})


const formRules = {
    name: [
        {
            required: true,
            message: '请输入名称',
            trigger: ['blur']
        }
    ],
    partner_id :[
        {
            required: true,
            message: '请选择合伙人',
            trigger: ['blur']
        }
    ],
    team_slogan: [
        {
            required: true,
            message: '请输入标语',
            trigger: ['blur']
        }
    ],
    team_picture: [
        {
            required: true,
            message: '请上传图片',
            trigger: ['blur']
        }
    ]
}

const { optionsData } = useDictOptions<{
    partner_cate: any[]
}>({
    partner_cate: {
        api: partnerCateAll
    }
})

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await teamEdit(formData) : await teamAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await teamDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
